<?php
namespace Components\User\Sha;

class Facade {
  public function pager($query) {
    if(!isset($query['page'])) {
      $query['page'] = 1;
    } else if($query['page'] <= 0)  {
      $query['page'] = 1;
    } else {
      $query['page'] = abs($query['page']);
    }

    if(!isset($query['limit'])) {
      $query['limit'] = 10;
    } else if($query['limit'] <= 0)  {
      $query['limit'] = 10;
    } else {
      $query['limit'] = abs($query['limit']);
    }

    return (new Logic()) -> pager($query);
  }

  public function findFirstByAccount($account) {
    if(is_null($account) || empty($account)) { return ['code'=>-1,'msg'=>'用户不存在!']; }

    $resUser = (new Service()) -> findFirst([
      'conditions'=>'account = '.$account,
      'columns' => 'name'
    ]);
    if($resUser['code'] != 0) { return ['code'=>-1,'msg'=>'用户不存在!']; }

    return ['code'=>0,'msg'=>'Success','user'=>$resUser['model']];
  }

  public function updateLoginPasswordByToken($token, $data) {
    $checkToken = Auth::checkToken($token);
    if($checkToken['code'] != 0) { return $checkToken; }

    $service = new Service();
    $resUser = $service -> findFirst(['conditions'=>'id = '.$checkToken['payload']['user']['id']]);
    if($resUser['code'] != 0) { return $resUser; }

    if($data['old_login_password'] === $data['login_password']) { return ['code'=>-1,'msg'=>'不能与原登陆密码一致']; }

    $validation = new Validation();
    $checkOldLoginPassword = $validation -> checkLoginPassword($data['old_login_password'], $resUser['model'] -> login_password);
    if($checkOldLoginPassword['code'] != 0) { return ['code'=>$checkOldLoginPassword['code'],'msg'=>'原登陆密码错误']; }

    $checkSafePassword = $validation -> checkSafePassword($data['safe_password'], $resUser['model'] -> safe_password);
    if($checkSafePassword['code'] != 0) { return ['code'=>$checkSafePassword['code'],'msg'=>'安全密码错误']; }

    $resUser['model'] -> login_password = Auth::getLoginPassword($data['login_password']);
    $resUser['model'] -> last_update_time = time();
    if(!$resUser['model'] -> update()) { return ['code'=>-1, 'msg'=>'修改失败'];}
    return ['code'=>0,'msg'=>'修改成功'];
  }

  public function updateSafePasswordByToken($token, $data) {
    $checkToken = Auth::checkToken($token);
    if($checkToken['code'] != 0) { return $checkToken; }

    $service = new Service();
    $resUser = $service -> findFirst(['conditions'=>'id = '.$checkToken['payload']['user']['id']]);
    if($resUser['code'] != 0) { return $resUser; }

    if($data['old_safe_password'] === $data['safe_password']) { return ['code'=>-1,'msg'=>'不能与原交易密码一致']; }
    
    $validation = new Validation();
    $checkOldSafePassword = $validation -> checkSafePassword($data['old_safe_password'], $resUser['model'] -> safe_password);
    if($checkOldSafePassword['code'] != 0) { return ['code'=>$checkOldSafePassword['code'],'msg'=>'原安全密码错误']; }

    $checkLoginPassword = $validation -> checkLoginPassword($data['login_password'], $resUser['model'] -> login_password);
    if($checkLoginPassword['code'] != 0) { return ['code'=>$checkLoginPassword['code'],'msg'=>'登陆密码错误']; }

    $resUser['model'] -> safe_password = Auth::getLoginPassword($data['safe_password']);
    $resUser['model'] -> last_update_time = time();
    if(!$resUser['model'] -> update()) { return ['code'=>-1, 'msg'=>'修改失败'];}
    return ['code'=>0,'msg'=>'修改成功'];
  }

  public function findFirstByToken($token) {
    $checkToken = Auth::checkToken($token);
    if($checkToken['code'] != 0) { return $checkToken; }

    $service = new Service();
    $resUser = $service -> findFirstByIdUseBackendAndUnsetPassword($checkToken['payload']['user']['id']);
    if($resUser['code'] != 0) { return $resUser; }

    $checkStatus = (new Validation()) -> checkStatus($resUser['model']['status']);
    if($checkStatus['code'] != 0) { return $checkStatus; }

    $resWallet = (new Wallet\Service()) -> findFirstById($resUser['model']['id']);
    if($resWallet['code'] != 0) { return $resWallet; }

    $pUser = [
      'cellphone' => '无',
      'cloud_account' => '无',
      'name' => '无',
      'account' => '无'
    ];
    if($resUser['model']['pid'] != 0) {
      $resPUser = $service -> findFirstByIdUseBackendAndUnsetPassword($resUser['model']['pid']);
      if($resPUser['code'] === 0) {
        $pUser['cellphone'] = $resPUser['model']['cellphone'];
        $pUser['cloud_account'] = $resPUser['model']['cloud_account'];
        $pUser['name'] = $resPUser['model']['name'];
        $pUser['account'] = $resPUser['model']['account'];
      }
    }

    return ['code'=>0,'msg'=>'成功','user'=>$resUser['model'],'p_user'=>$pUser,'user_wallet'=>$resWallet['model']];
  }

  public function createHasPAccount($data) {
    if(!isset($data['p_account'])) { return ['code'=>-1,'msg'=>'推荐人不存在']; }
    $findFirstPUser = Model::findFirst(['account = "' . $data['p_account'] . '"']);
    if(!$findFirstPUser) { return ['code'=>-1,'msg'=>'推荐人不存在']; }
    $data['pid'] = $findFirstPUser -> id;

    $findCloudUser = Model::find(['cloud_account = "' . $data['cloud_account'] . '"']);
    foreach($findCloudUser as $cloudUser) {
      if($cloudUser -> status == 4) { return ['code'=>-1,'msg'=>'您的云付通已在黑名单里']; }
    }
    if($findCloudUser -> count() >= 3) { return ['code'=>-1,'msg'=>'1个云付通账号只能绑定3个账号']; }
    unset($data['p_account']);

    $findFirstPUser -> p_user_total += 1;
    $findFirstPUser -> update();

    return (new Logic) -> create($data);
  }

  public function encryptTokenByLogin($data) {
    $res = (new Logic()) -> login($data);
    if($res['code'] != 0) { return $res; }

    return ['code'=>0,'msg'=>'登陆成功','token'=>(new Auth()) -> encryptToken([
      'user' => ['id'=>$res['model'] -> id],
      'exp' => time() + 172800
    ])];
  }
}
